export function didMount() {
    console.log("「页面 JS」：当前页面地址 " + location.href);
    debugger;

    const self = this;

    const elements = {
        punishmentCount: self.$$("container_6aelur"),
        ordinaryProcedure: self.$$("container_dnu90k"),
        summaryProcedure: self.$$("container_8kwn1c"),
        decideType1: self.$$("text_8z8ken"),
        decideType2: self.$$("text_gcm45d"),
        decideType3: self.$$("text_fd0zj3"),
        decideType4: self.$$("text_dw7ijp"),
        decideType5: self.$$("text_9gd89m"),
        decideType6: self.$$("text_1oegym"),
        decideType7: self.$$("text_hkfhcw"),
        decideType8: self.$$("text_41ci0m"),
    };

    const listTitle = self.$$("text_bkttpg");
    const pageTableList = self.$$("page_widget_list_6ndvbc");

    var TITLE_MAP = {
        punishmentCount: '行政处罚预警清单',
        ordinaryProcedure: '普通程序预警清单',
        summaryProcedure: '简易程序预警清单',
        decideType1: function() {
            var element = document.getElementById("entityValidityName");
            return element && element.textContent ? element.textContent + '预警清单' : '主体合法性预警清单';
        },
        decideType2: function() {
            var element = document.getElementById("procedureStandardName");
            return element && element.textContent ? element.textContent + '预警清单' : '程序规范性预警清单';
        },
        decideType3: function() {
            var element = document.getElementById("evidenceQualityName");
            return element && element.textContent ? element.textContent + '预警清单' : '证据质量预警清单';
        },
        decideType4: function() {
            var element = document.getElementById("documentArchiveName");
            return element && element.textContent ? element.textContent + '预警清单' : '文书档案预警清单';
        },
        decideType5: function() {
            var element = document.getElementById("discretionExerciseName");
            return element && element.textContent ? element.textContent + '预警清单' : '裁量行使预警清单';
        },
        decideType6: function() {
            var element = document.getElementById("enforcementImpactName");
            return element && element.textContent ? element.textContent + '预警清单' : '执法影响预警清单';
        },
        decideType7: function() {
            var element = document.getElementById("infoDisclosureName");
            return element && element.textContent ? element.textContent + '预警清单' : '信息公开预警清单';
        },
        decideType8: function() {
            var element = document.getElementById("integrityDisciplineName");
            return element && element.textContent ? element.textContent + '预警清单' : '廉洁与纪律预警清单';
        }
    };

    // 按钮样式设置
    function setButtonStyle(element) {
        if (!element || !element.$el) return;

        var $el = element.$el;
        $el.style.cursor = 'pointer';
        $el.style.transition = 'all 0.2s ease';

        $el.addEventListener('mouseenter', function() {
            $el.style.transform = 'scale(1.02)';
            $el.style.boxShadow = '0 2px 8px rgba(0,0,0,0.1)';
        });

        $el.addEventListener('mouseleave', function() {
            $el.style.transform = 'scale(1)';
            $el.style.boxShadow = 'none';
        });
    }

    // 获取规则ID
    function getRuleId(element) {
        if (!element) return null;
        if (element.dataset && element.dataset.ruleId) return element.dataset.ruleId;
        return getRuleId(element.parentElement);
    }

    // 获取地区值
    function getRegionValues() {
        return {
            countryId: self.$$("text_input_6iltos") ? self.$$("text_input_6iltos").cValue : null,
            provinceId: self.$$("text_input_e7ln66") ? self.$$("text_input_e7ln66").cValue : null,
            cityId: self.$$("text_input_1x0udy") ? self.$$("text_input_1x0udy").cValue : null,
            districtId: self.$$("text_input_55q4pw") ? self.$$("text_input_55q4pw").cValue : null,
            streetId: self.$$("text_input_dd3dav") ? self.$$("text_input_dd3dav").cValue : null,
            communityId: self.$$("text_input_ayzvd0") ? self.$$("text_input_ayzvd0").cValue : null
        };
    }

    // 按钮点击处理
    function handleButtonClick(buttonName, event) {
        debugger;

        var title = typeof TITLE_MAP[buttonName] === 'function'
            ? TITLE_MAP[buttonName]()
            : TITLE_MAP[buttonName];

        if (!listTitle || !title) return;

        console.log(buttonName + "按钮被点击");
        listTitle.$$setValue(title);

        var filterParams = getRegionValues();
        filterParams.ruleId = null;
        filterParams.bizId = null;

        // 设置过滤参数
        if (buttonName.indexOf('decideType') === 0) {
            filterParams.ruleId = getRuleId(event.target);
        } else {
            filterParams.bizId =
                buttonName === 'punishmentCount' ? '1' :
                    buttonName === 'ordinaryProcedure' ? '2' : '1,2';
        }

        filterList(pageTableList, filterParams);
    }

    // 应用按钮样式和事件
    Object.keys(elements).forEach(function(key) {
        var element = elements[key];
        if (!element || !element.$el) return;

        setButtonStyle(element);
        element.$el.addEventListener("click", function(event) {
            handleButtonClick(key, event);
        });
    });
}


function filterList(pageTableList, filterParams) {
    if (!pageTableList || !pageTableList.dataEngine || !pageTableList.dataEngine.state) return;

    // 设置额外参数
    if (pageTableList.$children && pageTableList.$children[0] && pageTableList.$children[0].$$setExtraParams) {
        pageTableList.$children[0].$$setExtraParams(filterParams);
    }

    // 刷新表格数据
    var tableComp = pageTableList.dataEngine.state.getElement("table_ycrya");
    if (tableComp && tableComp.$$refreshData) {
        tableComp.$$refreshData();
    }
}
